var app = getApp();
Page({
    data:{
        inited : false,
        helplist : [],
        page : {
            isend : false,
            doing : false,
            doo : 'shopinfo_v1',
            pdata : {
                op : 'goodlist',
                page : 1,
                shopid : 0,
                search : '',
            },
            waitf : 0,
            nodataf : 0,
        },
        datalist : [],
        current : 0,
		siteroot : app.siteInfo.siteroot.replace('app/index.php', ''),uniacid: app.siteInfo.uniacid,
    },
    onLoad:function(options){
        var self = this;
        var id = options.id ? options.id : (options.scene ? options.scene : '');
        var zfid = options.zfid ? options.zfid : '';
        
        app.com.http('signin','POST',{id:id,op:'info',zfid:zfid},0,false,'','',function(res){
            if (!res.data.errno && res.data ) {
                self.setData({
                    options : options,
                    Pagetheme : res.data.data.params.style,
                    signinfo : res.data.data.signinfo,
                    signstep : res.data.data.signstep,
                    shop : res.data.data.shop,
                    inited : true,
                    sharedata : res.data.data.sharedata,
                    'page.pdata.shopid' : res.data.data.signinfo.shopid,
                    showtake : res.data.data.signinfo.isneedtaked == 1 ? 1 : 0,
                },function(){
                    self.onReachBottom();
                });
                // 关闭下拉刷新
                wx.stopPullDownRefresh();

                wx.setNavigationBarTitle({
                  title:  res.data.data.signinfo.name,
                });

            }else{
                app.com.alert(res.data.message,function(){
                    app.com.backPage(options.zfid);
                });
            }
        });
       
    },
    onShareAppMessage : function(){
        var self = this;
        return {
            title: self.data.sharedata.title,
            path: self.data.sharedata.path,
            imageUrl : self.data.sharedata.img,
        }
    },
    onPullDownRefresh: function () {
        if( this.data.isdown ) return false;
        this.setData({
            isdown : true,
        });
        
        this.onLoad( this.data.options );
        this.setData({
            isdown : false,
        });
    },
    showRules : function(){
        this.setData({
            showRules : true,
        });
    },
    hideRules : function(){
        this.setData({
            showRules : false,
        });
    },
    onReachBottom : function(){
        var self = this;
        app.com.getPage(self,self.data.page,20,function(res){
            self.setData({
                datalist : self.data.datalist.concat(res.data.data.list)
            });
        });
    },
    change(e){
        this.setData({
            current: e.detail.current
        })
    },
    signin(){
        var self = this;
        var postdata = {
            op : 'signtoday',
            signid : self.data.signinfo.id
        }
        app.com.http('signin','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno) {
                self.setData({
                    signin:true,
                    logid : res.data.data.sid,
                    qrcode : app.com.murl('img',{op:'signin',sid:res.data.data.sid,m:'zofui_sales'}),
                },function(){
                    self.checkSigned();
                });
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    checkSigned(){
        var self = this;
        setTimeout(function(){
            if( !self.data.signin ) return false;
            
            var postdata = {
                op : 'checksign',
                logid : self.data.logid
            }
            app.com.http('signin','POST',postdata,0,true,'','',function(res){
                if (!res.data.errno && res.data) {
                    self.setData({
                        signin:false,
                    });
                    if( res.data.data.issuc == 1 && res.data.data.isselftake == 1 ){

                        self.setData({
                            showtake : true,
                        })
                    }else{
                        app.com.alert(res.data.message,function(){
                            self.onPullDownRefresh();
                        });
                    }
                }else{
                    self.checkSigned();
                }
            });
        },3000)
    },
    hidesignin(){
        this.setData({signin:false});
    },
    takeprize(){
        var self = this;
        var postdata = {
            op : 'takeprize',
            signid : self.data.signinfo.id
        }
        app.com.http('signin','POST',postdata,0,true,'','',function(res){
            if (!res.data.errno) {
                app.com.alert(res.data.message,function(){
                    self.setData({
                        showtake : false,
                    },function(){
                        self.onPullDownRefresh();
                    });
                });
            }else{
                app.com.alert(res.data.message);
            }
        });
    },
    location : function( e ){
        app.com.location( e );
    },
    otherapp : function( e ){
        app.com.otherapp( e );
    },
    navigateto : function( e ){
        app.com.navigateto( e );
    },
    redirectto : function( e ){
        app.com.redirectto( e );
    },    
    callphone : function(e){
        app.com.callphone( e );
    },
    toweburl : function(e){
        app.com.toweburl( e );
    },
    showimages : function( e ){
        app.com.showimages( e );
    },
    switchTab(e){
        wx.switchTab({
            url: e.currentTarget.dataset.url
        });
    },
    footNavigate(e){
        var url = e.currentTarget.dataset.url;
        wx.switchTab({
          url: url
        })
    },
    FormIdsubmit : function(e){
        if( app.globalData.formarr.length <= 20 ) app.globalData.formarr.push(e.detail.formId);
    },

})